
package com.tiancheng.onecode.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

/**
 * application 签名生成
 */
public class AppAccessTokenSignatureUtils {
    /**
     * 签名
     */
    public static String signature(String... param) {
        Set<String> signatureVar = new TreeSet<>(Arrays.asList(param));
        StringBuilder signatureStr = new StringBuilder();
        for (String key : signatureVar) {
            signatureStr.append(key);
        }
        return DigestUtils.sha256Hex(signatureStr.toString());
    }


    public static void main(String[] args) {
        String clientId = "6746b567cdb2c3df2282dbeb";
        String clientSecret = "1f2443021c04d592cb3a59cdc274f7e0";
        String rand = UUID.randomUUID().toString().substring(0, 8);
        final String currentTimeMillis = Long.toString(System.currentTimeMillis());
        System.out.println("clientId=" + clientId);
        System.out.println("clientSecret=" + clientSecret);
        System.out.println("rand=" + rand);
        System.out.println("timestamp=" + currentTimeMillis);


        Set<String> signatureVar = new TreeSet<>(Arrays.asList(clientId, clientSecret, rand, currentTimeMillis));
        StringBuilder signatureStr = new StringBuilder();
        for (String key : signatureVar) {
            signatureStr.append(key);
        }
        System.out.println("signature源字符=" + signatureStr.toString());
        String signature = DigestUtils.sha256Hex(signatureStr.toString());
        System.out.println("signature=" + signature);
    }

}
